loadstring(exports.dgs:dgsImportFunction())()

local sw,sh = guiGetScreenSize()
local px,py = sw/1366,sh/768
cmtaTUNING = false
font = dgsCreateFont( ":cmta-pobieraniezasobow/OSB.ttf", 12*px )

local spoilers={
	[1001]="1001",
	[1002]="1002",
	[1003]="1003",
	[1014]="1014",
	[1015]="1015",
	[1023]="1023",
	[1049]="1049",
	[1050]="1050",
    [1138]="1038",
	[1139]="1139",
    [1146]="1146",
	[1147]="1147",
	[1158]="1158",
	[1162]="1162",
	[1164]="1164",
}

for i,v in pairs(spoilers) do
	local txd = engineLoadTXD("spl_im_pos_tor.txd")
	engineImportTXD(txd, i)
	local dff = engineLoadDFF("spoilers/"..i..".dff", i)
	engineReplaceModel(dff, i)
end

TuningVehicleOffsets ={

     [418] = { -- moonbeam
        {1000, 0, -1.6, 0.9};
        {1001, 0, -1.6, 0.9};
        {1002, 0, -1.6, 0.9};
        {1003, 0, -1.6, 0.9};
        {1014, 0, -1.6, 0.9};
        {1015, 0, -1.6, 0.9};
        {1016, 0, -1.6, 0.9};
        {1023, 0, -1.6, 0.9};
        {1049, 0, -1.6, 0.9, 0.8};
        {1050, 0, -1.6, 0.9};
        {1058, 0, -1.6, 0.9};
        {1060, 0, -1.6, 0.9};
        {1138, 0, -1.6, 0.9, 0.8};
        {1139, 0, -1.6, 0.9};
        {1146, 0, -1.6, 0.9};
        {1147, 0, -1.6, 0.9, 0.9};
        {1158, 0, -1.6, 0.9, 0.75};
        {1162, 0, -1.6, 0.9, 0.8};
        {1163, 0, -1.6, 0.9};
        {1164, 0, -1.6, 1, 0.8};
     };
     [445] = { -- admiral
        {1000, 0, -2.6, 0.2};
        {1001, 0, -2.6, 0.2};
        {1002, 0, -2.6, 0.2};
        {1003, 0, -2.6, 0.2};
        {1014, 0, -2.6, 0.2};
        {1015, 0, -2.6, 0.2};
        {1016, 0, -2.6, 0.2};
        {1023, 0, -2.6, 0.2};
        {1049, 0, -2.6, 0.2};
        {1050, 0, -2.6, 0.2};
        {1058, 0, -2.6, 0.2};
        {1060, 0, -2.6, 0.2};
        {1138, 0, -2.6, 0.2, 0.8};
        {1139, 0, -2.6, 0.2};
        {1146, 0, -2.6, 0.2};
        {1147, 0, -2.6, 0.2, 0.9};
        {1158, 0, -2.6, 0.2, 0.75};
        {1162, 0, -2.6, 0.2, 0.8};
        {1163, 0, -2.6, 0.2};
        {1164, 0, -2.6, 0.3, 0.8};
     };

}

addEvent("cmta_tuning:showGUI", true)
addEventHandler("cmta_tuning:showGUI", resourceRoot, function()
    cmtaTUNING = true
    showCursor(true)
    addEventHandler ( "onDgsMouseClick", getRootElement(), onclick)
    window = dgsCreateWindow(1100*px,250*py,250*px,500*py,"Tuning Wizualny - Spojlery",false)
    dgsWindowSetCloseButtonEnabled(window, false)
    dgsWindowSetMovable (window, false )
    dgsWindowSetSizable (window, false )
    gridbuy = dgsCreateGridList (0*px, 0*py, 250*px, 290*py, false,window )
    dgsGridListAddColumn( gridbuy, "Elementy Tuningu", 0.95 ) 
    dgsGridListSetAutoSortEnabled(gridbuy,false)
    button = dgsCreateButton( 25*px, 300*py, 200*px, 40*py, "Podgląd", false,window,false,false,false,false,false,false,false,tocolor(90,90,90),tocolor(210,105,30,255))
    button2 = dgsCreateButton( 25*px, 350*py, 200*px, 40*py, "Kup", false,window,false,false,false,false,false,false,false,tocolor(90,90,90),tocolor(210,105,30,255))
    button3 = dgsCreateButton( 25*px, 400*py, 200*px, 40*py, "Zamknij", false,window,false,false,false,false,false,false,false,tocolor(90,90,90),tocolor(210,105,30,255))

    creategui()
end)

addEvent("cmta_tuning:hideGUI", true)
addEventHandler("cmta_tuning:hideGUI", resourceRoot, function()
    cmtaTUNING = false
    showCursor(false)
    removeEventHandler ( "onDgsMouseClick", getRootElement(), onclick)
    if isElement(window) then
    destroyElement(window)
    end
    if isElement(spojler) then
        destroyElement(spojler)
    end
end)

function creategui()
    for i,v in pairs(TuningVehicleOffsets[getElementModel(getPedOccupiedVehicle(localPlayer))]) do
        local row = dgsGridListAddRow ( gridbuy )
        dgsGridListSetItemText ( gridbuy, row, 1, v[1] )
    end

end

function onclick(btn,state)
    if btn=="left" and state=="down" and cmtaTUNING then
    if source == button then
        if isElement(spojler) then
            destroyElement(spojler)
        end
    local selected = dgsGridListGetSelectedItem(gridbuy)
	if selected ~=-1 then
    local ulepid = dgsGridListGetItemText(gridbuy,selected,1)  
    for i,v in pairs(TuningVehicleOffsets[getElementModel(getPedOccupiedVehicle(localPlayer))]) do
        if tonumber(ulepid) == v[1] then
            offs1 = v[2]
            offs2 = v[3]
            offs3 = v[4]
            size = v[5] or 1

        end
    end                              
    spojler = createObject( ulepid, -1745.15491, -137.97495, 3.55469+0.60)

    setObjectScale(spojler,size)
    attachElements ( spojler, getPlayerOccupiedVehicle(localPlayer), offs1,offs2,offs3)
    end
    end
    if source == button3 then
        cmtaTUNING = false
        showCursor(false)
        removeEventHandler ( "onDgsMouseClick", getRootElement(), onclick)
        destroyElement(window)
        if isElement(spojler) then
       ---     destroyElement(spojler)
        end
    end
    end
end